﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BanditGame2.WCFService;

namespace BanditGame2
{
    public partial class ChangeUserDetailsWindow : ChildWindow
    {
        public ChangeUserDetailsWindow()
        {
            InitializeComponent();
            WCFService.WCFServiceClient LAPClient = new WCFService.WCFServiceClient();
            LAPClient.getUserDetailsCompleted += new EventHandler<WCFService.getUserDetailsCompletedEventArgs>(getUserDetails_Completed);
            LAPClient.getUserDetailsAsync(App.currentUserLogin);
        }

        private void getUserDetails_Completed(object sender, getUserDetailsCompletedEventArgs e)
        {
            tbxName.Text = e.Result.Jmeno;
            tbxSurname.Text = e.Result.Prijmeni;
            tbxEmail.Text = e.Result.Email;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            tbError.Text = "";
            tbNameStar.Text = "";
            tbSurnameStar.Text = "";
            tbEmailStar.Text = "";
            tbPasswordStar.Text = "";
            tbPasswordNewStar.Text = "";
            tbNewPassword2Star.Text = "";
            

            bool allFilled = true;
            if (tbxName.Text == "")
            {
                tbNameStar.Text = "*";
                allFilled = false;
            }
            if (tbxSurname.Text == "")
            {
                tbSurnameStar.Text = "*";
                allFilled = false;
            }
            if (tbxEmail.Text == "")
            {
                tbEmailStar.Text = "*";
                allFilled = false;
            }
            if (tbxPassword.Password == "")
            {
                tbPasswordStar.Text = "*";
                allFilled = false;
            }
            if (tbxNewPassword.Password == "")
            {
                tbPasswordNewStar.Text = "*";
                allFilled = false;
            }
            if (tbxNewPassword2.Password == "")
            {
                tbNewPassword2Star.Text = "*";
                allFilled = false;
            }
            if (tbxNewPassword.Password != tbxNewPassword2.Password)
            {
                tbError.Text = "Hesla se neshodují.";
                tbxNewPassword.Password = "";
                tbxNewPassword2.Password = "";
                allFilled = false;
            }
            if (allFilled)
            {

                WCFService.WCFServiceClient LAPClient = new WCFService.WCFServiceClient();
                LAPClient.getLoginMatchCompleted +=
                    new EventHandler<WCFService.getLoginMatchCompletedEventArgs>(CheckPassword_Completed);
                LAPClient.getLoginMatchAsync(App.currentUserLogin, tbxPassword.Password);
                
            }
        }

        private void CheckPassword_Completed(object sender, getLoginMatchCompletedEventArgs e)
        {
            if (e.Result == 1)
            {
                User user = new User();
                user.Login = App.currentUserLogin;
                user.Jmeno = tbxName.Text;
                user.Prijmeni = tbxSurname.Text;
                user.Email = tbxEmail.Text;
                user.Password = tbxNewPassword.Password;
                WCFService.WCFServiceClient LAPClient = new WCFService.WCFServiceClient();
                LAPClient.updateUserCompleted +=
                    new EventHandler<WCFService.updateUserCompletedEventArgs>(Update_Completed);
                LAPClient.updateUserAsync(user);
            }
            else
            {
                tbError.Text = "Špatné heslo.";
            }

        }

        private void Update_Completed(object sender, updateUserCompletedEventArgs e)
        {
            if (e.Result)
            {
                
                this.DialogResult = true;
            }
            else
            {
                tbError.Text = "Někde se stala chyba";
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

